class Solution {
public:

    vector<int> ret;
    bool dfs(int cur,vector<bool>& vis,vector<vector<int>>& rooms)
    {
        if(ret.size() == rooms.size())
        {
            return true;
        }

        for(int i = 0;i < rooms[cur].size();i++)
        {
            int next = rooms[cur][i];
            if(vis[next]) continue;
            vis[next] = true;
            ret.push_back(next);
            if(dfs(next,vis,rooms))
            {
                return true;
            }
            // ret.pop_back();
            // vis[next] = false;
        }
        return false;
    }

    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        vector<bool> vis(rooms.size(),false);
        vis[0] = true;
        ret.push_back(0);

        return dfs(0,vis,rooms);
    }
};